<template>
	<view class="manage-bigbox">
		<view class="search-wrap flex rcb">
			<view class="search-input-view flex rcs">
				<text class="search-icon cuIcon-search"></text>
				<input type="text" placeholder="请输入名称搜索" v-model="name" class="ipt search-input" />
			</view>
			<view class="btn-item search-btn-item" @tap="handleClickConfirm">搜索</view>
		</view>
		<Loading v-if="!ready" />
		<view class="add-padding" v-else-if="list.length > 0">
			<view class="manage-list flex" v-for="(item,index) in list" :key="index">
				<view class="manage-list-title">{{item.name}}</view>
				<view class="flex">
					<view class="delect-btn" @tap="handleClickDel(item.id)"><img src="../../static/icon/icon_trash.png" alt="" class="img-sty"></view>
					<view class="change-btn" @tap="handleClickChange(item)"><img src="../../static/icon/icon_edit.png" alt="" class="img-sty"></view>
				</view>
			</view>
		</view>
		<Nothing v-else />
		<view class="bottom-btn" @tap="handleClickAdd">+新增</view>
		<view class="cu-modal show" v-if="showConfirm">
		    <view class="cu-dialog dialog-container">
		        <view class="cu-bar dialog-title-view flex rcb">
		            <view class="dialog-title-style">温馨提示</view>
		            <view class="dialog-btn-close" @tap="onPressCancel">
		                <text class="cuIcon-close"></text>
		            </view>
		        </view>
		        <view class="dialog-content-view flex ccc">
		            <view class="dialog-context flex ccc">确定要删除吗？</view>
		        </view>
		        <view class="cu-bar dialog-btn-wrap flex rcb">
		            <view class="btn-item btn-cancel-item flex ccc" @tap="onPressCancel">取消</view>
		            <view class="btn-item btn-submit-item flex ccc" @tap="onPressSubmit">确定</view>
		        </view>
		    </view>
		</view>
	</view>
</template>

<script>
	import Nothing from '../../components/common/no-thing.vue'
	import Loading from '../../components/common/loading.vue'
	export default {
		components: {
			Nothing,
			Loading,
		},
		data() {
			return {
				showConfirm: false,
				ready: false,
				item_id: '',
				msg: '',
				name: '',
				token: '',
				page: 1,
				list: [],
			}
		},
		methods: {
			onPressCancel() {
			    this.showConfirm = false;
			},
			async onPressSubmit() {
			    let url = this.ServicesApi.delSubsidy;
			    let data = {
			        id: this.item_id,
			    };
			    let result = await this.Services.post(url, data);
			    if (result.code == this.StatusCode.SUCCESS_CODE) {
			        uni.showToast({
			            icon: 'none',
						title: result.msg,
			        });
			        this.showConfirm = false;
			        this.getList();
			    } else {
			        uni.showToast({
			            icon: 'none',
			            title: result.msg,
			        });
			    }
			},
			// 点确定
			handleClickConfirm() {
				this.getList();
			},
			// 取token
			getToken() {
				try {
					const value = uni.getStorageSync('login_key');
					if (value) {
						this.token = value;
					}
				} catch (e) {
					// error
				}
			},
			// 点击搜索
			hanndleClickSearch() {
				this.getList()
			},
			// 点击新增跳转新增页面
			handleClickAdd() {
				uni.navigateTo({
					url: '/pages/publish/addSubsidy'
				})
			},
			// 点击修改跳页面传参数
			handleClickChange(item) {
				let items = JSON.stringify(item)
				// console.log(id);
				uni.navigateTo({
					url: '/pages/publish/addSubsidy?item=' + items
				})
			},
			// 点击删除 ==>加提示框
			async handleClickDel(id) {
				this.item_id = id;
				this.showConfirm = true;
				return;
				this.getToken()
				// 发请求
				uni.showModal({
					title: '温馨提示',
					content: '确认要删除吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: async res => {
						if (res.confirm) {
							let url = this.ServicesApi.delSubsidy;
							let data = {
								id,
							};
							let result = await this.Services.post(url, data);
							if (result.code == this.StatusCode.SUCCESS_CODE) {
								uni.showToast({
									icon: 'none',
									title: result.msg,
								});
								this.getList();
							} else {
								uni.showToast({
									icon: 'none',
									title: result.msg,
								});
							}

							// uni.request({
							//     url: , 
							// 	method:'POST',
							// 	header: {
							// 		"content-type":"application/x-www-form-urlencoded"
							// 	},
							//     data: {
							// 		token:this.token,
							// 		id:id,
							//     },
							//     success: (res) => {
							// 		// console.log(res.data)
							// 		if(res.data.code == 0){
							// 			uni.showToast({
							// 				icon: 'none',
							// 				title: res.data.msg,
							// 			});
							// 			return false
							// 		}else{
							// 			// uni.navigateTo({
							// 			// 	url:'./companyType'
							// 			// })
							// 			uni.showToast({
							// 				title: res.data.msg
							// 			});
							// 		}
							// 		this.getList()
							//     }
							// });
						} else if (res.cancel) {
							// uni.showToast({
							// 	title: '取消'
							// });
						}
					}
				});

			},
			// 请求列表参数
			async getList() {
				this.getToken()
				// 发请求
				let url = this.ServicesApi.subsidyList;
				let data = {
					limit: 50,
					page: this.page,
					name: this.name, //岗位类型名称
				};
				let result = await this.Services.post(url, data);
				this.ready = true;
				if (result.code == this.StatusCode.SUCCESS_CODE) {
					this.list = result.data.data;
				} else {
					uni.showToast({
						icon: 'none',
						title: result.msg,
					});
				}

				// uni.request({
				//     url: this.ServicesApi.subsidyList, 
				// 	method:'GET',
				// 	header: {
				// 		"content-type":"application/x-www-form-urlencoded"
				// 	},
				//     data: {
				// 		token:this.token,
				// 		page:this.page,
				// 		limit:50,
				// 		name:this.name,//岗位类型名称
				//     },
				//     success: (res) => {
				// 		// console.log(res.data)
				// 		this.list = res.data.data.data;
				//     }
				// });
			},
		},
		mounted() {
			this.getList()
		},
		onShow: function(e) {
			// #ifndef H5
			let pages = getCurrentPages();
			let currPage = pages[pages.length - 1];
			if (currPage.data && currPage.data.isDoRefresh == true) {
				currPage.data.isDoRefresh = false;
				// console.log('s刷新');
				this.getList()
			} else {
				//不用刷新
			}
			// #endif
			// #ifdef H5
			this.getList();
			// #endif
		}
	}
</script>

<style scoped lang="stylus">
	
	.search-wrap {
		z-index: 99;
		height: 100rpx;
		padding: 0 30rpx;
		/* #ifndef H5 */
			top: 0;
		/* #endif */
		left: 0;
		right: 0;
		position: fixed;
		background: #e7e7e7;
		.search-input-view {
			flex: 1;
			height: 70rpx;
			padding: 0 20rpx;
			margin-right: 20rpx;
			border-radius: 10rpx;
			background-color: #FFFFFF;
			.search-icon {
				color: #999999;
				font-size: 32rpx;
				margin-right: 10rpx;
			}
			.search-input {
				flex: 1;
				text-indent: 20rpx;
				margin-right: 20rpx;
			}
		}
	
		.search-btn-item {}
	}
	.ipt {
		background: #fff;
		border-radius: 8upx;
		margin: 30upx 0;
	}

	.search-btn {
		height: 50upx;
		width: 100upx;
		text-align: center;
		line-height: 50upx;
		font-size: 28upx;
		padding: 0;
	}

	.addbtn {
		height: 100upx;
		width: 300upx;
		margin: 30upx auto;
		text-align: center;
		line-height: 100upx;
		background: pink;
	}

	.sty {
		justify-content: space-around;
		padding: 10upx;
	}

	.msg {
		height: 50upx;
		color: red;
		font-size: 28upx;
	}

	.manage-bigbox {
		/* #ifndef H5 */
			margin-top: 24upx;
		/* #endif */

		.add-padding {
			padding: 100rpx 30rpx 30rpx;
			background: #fff;

			.manage-list {
				height: 108upx;
				align-items: center;
				justify-content: space-between;
				border-bottom: 1px solid #DBDBDB;

				.manage-list-title {
					font-size: 32upx;
					color: #333;
				}

				.delect-btn {
					margin-right: 44upx;
				}

				.img-sty {
					height: 38upx;
					width: 38upx;
				}
			}
		}

		.bottom-btn {
			height: 76upx;
			width: 440upx;
			background: linear-gradient(270deg, rgba(0, 106, 231, 1) 0%, rgba(66, 157, 255, 1) 100%);
			line-height: 76upx;
			text-align: center;
			color: #fff;
			font-size: 32upx;
			margin: 80upx auto;
		}
	}
</style>
